//
// Created by korifey on 06.07.2022.
//

#include "main.h"
#include "gpio.h"

#include "rotor-light.hpp"

extern "C" void SystemClock_Config(void);

extern "C" int64_t get_current_time(void);

namespace rl = rotor_light;


namespace message {
    struct Ping : rl::Message {
        using Message::Message;
        static constexpr auto type_id = __LINE__;
        rl::MessageTypeId get_type_id() const override { return type_id; }
    };

    struct Pong : rl::Message {
        using Message::Message;
        static constexpr auto type_id = __LINE__;
        rl::MessageTypeId get_type_id() const override { return type_id; }
    };
} // namespace message

struct Pinger : rl::Actor<2> {
    using Parent = Actor<2>;

    void initialize() override {
        subscribe(&Pinger::on_pong);
        Parent::initialize();
    }

    void advance_start() override {
        Parent::advance_start();
        ping();
    }

    void ping() {

        send<message::Ping>(0, ponger_id);
    }

    void on_pong(message::Pong &) {
        LL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
        add_event(
                500, [](void *data) { static_cast<Pinger *>(data)->ping(); }, this);
    }

    rl::ActorId ponger_id;
};

struct Ponger : rl::Actor<2> {
    using Parent = Actor<2>;

    void initialize() override {
        subscribe(&Ponger::on_ping);
        Parent::initialize();
    }
    void on_ping(message::Ping &) {
        LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
        add_event(
                200, [](void *data) { static_cast<Ponger *>(data)->reply(); }, this);
    }
    void reply(){send<message::Pong>(0, pinger_id);}
    rl::ActorId pinger_id;
};

using Supervisor =
        rl::Supervisor<rl::SupervisorBase::min_handlers_amount, Pinger, Ponger>;
using Storage = rl::traits::MessageStorage<rl::message::ChangeState,
        rl::message::ChangeStateAck,
        message::Ping, message::Pong>;
using Queue = rl::Queue<Storage, 5>; /* upto 5 messages in 1 queue */
using Planner = rl::Planner<2>;      /* upto 1 time event */

int main(int, char **) {

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    LL_APB4_GRP1_EnableClock(LL_APB4_GRP1_PERIPH_SYSCFG);

    /* System interrupt init*/
    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
    /* Update the time base */

    /* Configure the system clock */
    SystemClock_Config();
    MX_GPIO_Init();
    SysTick_Config(SystemCoreClock / 1000);


    /* allocate */
    Queue queue;
    Planner planner;
    rl::Context context{&queue, &planner, &get_current_time};
    Supervisor sup;

    /* setup */
    sup.bind(context);
    auto pinger = sup.get_child<0>();
    auto ponger = sup.get_child<1>();
    pinger->ponger_id = ponger->get_id();
    ponger->pinger_id = pinger->get_id();
    /* let it polls timer */
    sup.start(true);

    /* main cycle */
    sup.process();
    return 0;
}